package com.bosssoft.hr.train.j2se.util;/*
 * @Auther:hantianbing
 * @Date:2022/12/3
 * @Description:这是一个工具类，
 */

import com.bosssoft.hr.train.j2se.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @ClassName UtilsDemo
 * @Description 这是一个工具类
 * @Author hantianbing
 * @Date 2022/12/3 14:57
 **/
@Slf4j
public class UtilsDemo {
    /**
     * sort方法,排序之后是递增的
     */
    public void method1(List<Integer> list){
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer integer, Integer t1) {
                return integer - t1;
            }
        });
    }

    public void method2(){
        List<Integer> list = new ArrayList<>();
        /**
         * 用isEmpty可以判断是不是空、没有内容
         */
        log.info(String.valueOf(CollectionUtils.isEmpty(list)));

        /**
         * exist判断是不是包含知指定规则的数据
         */
        String[] langs = {"java", "python", "c"};
        CollectionUtils.exists(Arrays.asList(langs), new Predicate() {
            @Override
            public boolean evaluate(Object o) {
                return "python".equalsIgnoreCase(o.toString());
            }
        });

        /**
         * 合并数组
         */
        List<String> s = new ArrayList<>();
        CollectionUtils.addAll(s, langs);
    }

    /***
     * @Description 方法三
     *
     * @Param
     * @return void
     * @Date 2022/12/3 15:57
     * @Author hantianbing
     **/
    public void method3(){
        /**
         * asList
         */
        String[] s = {""};
        List<String> strings = Arrays.asList(s);

        /**
         * sort
         */
        int[] nums = new int[]{1,5,8,4,2};
        Arrays.sort(nums);
        for (int num : nums) {
            log.info(num+"");
        }

        /**
         * copyOf
         */
        int[] ints = Arrays.copyOf(nums, 4);
        log.info("-------------");
        for (int anInt : ints) {
            log.info(anInt+"");
        }

        /**
         * binarySearch
         */
        int i = Arrays.binarySearch(nums, 8);
        log.info("位置是  "+i);
    }

    /***
     * @Description 寻找文件目录
     *
     * @Param No such property: code for class: Script1
     * @return void
     * @Date 2022/12/3 16:08
     * @Author hantianbing
     **/
    public void method4(){
        Collection<File> files = FileUtils.listFiles(new File("/"), new String[]{"*.java"}, true);
        for (File file : files) {
            log.info(file.toString());
        }
    }

    /***
     * @Description 判断字符传是不是空， null 和“” 都是空
     *
     * @Param No such property: code for class: Script1
     * @return void
     * @Date 2022/12/3 16:13
     * @Author hantianbing
     **/
    public void method5(){
        log.info(String.valueOf(StringUtils.isEmpty("")));
        log.info(String.valueOf(StringUtils.isEmpty(null)));
        log.info(String.valueOf(StringUtils.isEmpty("1234")));
    }

    /***
     * @Description 复制对象
     *
     * @Param No such property: code for class: Script1
     * @return void
     * @Date 2022/12/3 16:23
     * @Author hantianbing
     **/
    public void method6(){
        User user = new User();
        user.setName("hello");
        User copyUser = new User();
        try {
            BeanUtils.copyProperties(copyUser, user);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        log.info(copyUser.getName());
    }

    public void method7() throws UnsupportedEncodingException {
        // MD5 加密  部分网站可以暴力破解
        String password="helloworld";
        byte[] bytePassword=password.getBytes("utf-8");
        DigestUtils.md5(bytePassword);
        // SH3 加密 安全性更好不可逆
        DigestUtils.sha3_256(bytePassword);

    }

}
